package com.swotxu.oldwebproject.session.listener.filter;

import com.swotxu.oldwebproject.session.http.RedisSessionRequestWrapper;
import com.swotxu.oldwebproject.session.http.RedisSessionResponseWrapper;
import com.swotxu.oldwebproject.utils.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 1、包装替换 req 和 res
 * 2、刷新会话
 *
 * @Date: 2021/3/25 0025 15:15
 * @Author: swotxu
 */
public class HttpServletWrapFilterListener implements RedisSessionFilterListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpServletWrapFilterListener.class);

    @Override
    public void doFilterBefore(RedisFilterEvent event) {
        HttpServletRequest request = event.getRequest();
        HttpServletResponse response = event.getResponse();

        RedisSessionRequestWrapper requestWrapper = new RedisSessionRequestWrapper(request);
        RedisSessionResponseWrapper responseWrapper = new RedisSessionResponseWrapper(response, requestWrapper);

        // 检查登录请求，刷新会话
        if (false) {
            SessionUtil.refreshSession(requestWrapper);
        }

        event.setRequest(requestWrapper);
        event.setResponse(responseWrapper);
    }

    @Override
    public int fliterOrder() {
        return -30;
    }
}
